<?php
/**
 * DMSellAction.class.php
 *二手房数据挖掘算法类
 *@author zz
 *@version 1
 *@since 2015-7-20
 */
header("Content-type: text/html; charset=utf-8");
ini_set("memory_limit","4000M");
ini_set('max_execution_time', '0');
include_once('upyun.class.php');
Class DMSellAction
{
    protected $upyun;
    
    protected $cityarea_item=array();
    
    protected $borough=array();
    
    protected $borough_byname=array();
    
    protected $house_sell_gov=array();
    
    protected $house_sell=array();
    
    protected $house_sell_info=array();
    
    protected $house_sell_pic=array();
    
    protected $house_sell_del=array();
    
    protected $broker_check=array();
    
    protected $auto=array();
    
	Public function index(){
	    echo "this is DMSell";
	}
	
	protected function __set_params($params){
	    $this->cityarea_item = M('city_item');
	    $this->borough = M('borough');
	    $this->borough_byname = M('borough_byname');
	    $this->house_sell_gov = M('house_sell_gov');
	    $this->house_sell = M('house_sell');
	    $this->house_sell_info = M('house_sell_info');
	    $this->house_sell_pic = M('house_sell_pic');
	    $this->house_sell_del = M('house_sell_del');
	    $this->broker_check = M('broker_check');
	    $this->auto=M('auto_increment_id');
	}
	
	
	/*@kevin
	 *@2016-12-18
	 *图片更新时才会调用
	 *
	 */
	function handlePic($pic,$id,$gov_id){
	    
	    $this->upyun = new UpYun('zhaijiso-pic', 'kys', 'zjsZJS123');
	    //房源图片表
	    $house_sell_pic = M('house_sell_pic');

        //选出所有gov_id对应的图片并去重
        $Pics = $house_sell_pic->field("pic_source_url")->where('gov_id='.$gov_id)->select();
	    $pics = [];
        foreach($Pics as $picc) {
            $pics[] = $picc["pic_source_url"];
        }
	    $picList = array_unique(explode('|',$pic));

        $pic_info = array();
	    foreach($picList as $k=>$value){
	        if(!empty($value)){
                //如果抓取到的照片链接已存在数据库中则跳过
                if(in_array($value,array_unique($pics))){continue;}
                $fhunit = file_get_contents($value);
	            $time = microtime(true)*10000;
	           
	            $ypy_path = '/sell/unit/'.$time.'.jpg';
	            $rsp1 = $this->upyun->writeFile($ypy_path, $fhunit, true); // 上传图片，自动创建目录
	            $endTime = microtime(true);
	            $pic_path = 'http://zhaijiso-pic.b0.upaiyun.com'.$ypy_path;
	            $pic_info[]=array(
	                'pic_source_url'=>$value,
	                'pic_url'=>$pic_path,
	                'house_sell_id'=>$id['id'],
	                'gov_id'=>$gov_id,
	                'pic_type'=>1,
	                'time'=>date('Y-m-d h:i:s',time())
	            );
	        }else{
	            continue;
	        }
	    }//dump($pic_info);
	    $house_sell_pic->addAll($pic_info);
	}
	
	
	/*
	 * 用其他经纪公司数据   对比 house_sell_info 表中的数据
	 *
	 * */
	public function fetch(){
        $city=$_GET['city'];
        $city=empty($city)?'beijing':$city;
        $this->__set_params();
	    $this->upyun = new UpYun('zhaijiso-pic', 'kys', 'zjsZJS123');
	    $list=array();
	    //gov表中is_contrast (2:未对比 ; 3: weight>=35; 5: 0<weight<35; 7: weight=0)
	    
	    $list=$this->house_sell_gov->where('is_contrast = 2 and is_fill=2 and source !=10 ')->limit("0,100")->select();
	    while(!empty($list)){
	        foreach($list as $key=>$value){
	            $result=array();
	            $value['created'] = microtime(true);
	            $value['updated'] = microtime(true);
	             
	            $value['house_floor'] = trimall($value['house_floor']);
	            //楼层处理
	            $this->handleFloor($value);
	            //$v    info 表中的数据
	            //$value  gov 表中的数据
	            if($this->result){
                    $arr_bath_insert = $this->countWeight($this->result,$value);
	            }
                //连接redis
                $this->connect_redis();
	            //判断是否有比对数据
	            if(!empty($arr_bath_insert)){

	                //有对比数据
	                $this->moreHouse($arr_bath_insert,$value);
                    //对比上的房源数beijing_2016_04_21_sell_$contrast_cnt
                    $date = date('Y-m-d',time());
                    $contrast_cnt = count($arr_bath_insert);
                    $redis_key = $city."_".$date."_sell_more_cnt";
                    if($this->redis->exists($redis_key)){
                        $this->redis->incrBy($redis_key, $contrast_cnt);
                    }else{
                        $this->redis->set($redis_key,$contrast_cnt);
                    }
	                unset($arr_bath_insert);

	            }else{
	                //无对比数据
                    //独立房源数beijing_2016_04_21_sell_alone_cnt
                    $date = date('Y-m-d',time());
                    $contrast_cnt = count($this->result);
                    $redis_key = $city."_".$date."_sell_alone_cnt";
                    if($this->redis->exists($redis_key)){
                        $this->redis->incrBy($redis_key, $contrast_cnt);
                    }else{
                        $this->redis->set($redis_key,$contrast_cnt);
                    }
	                $this->aloneHouse($this->result,$value);
	            }
	
	        }
	        $list=$this->house_sell_gov->where('is_contrast = 2 and is_fill=2 and source != 10')->limit("0,100")->select();
	    }
	}
	
	/*
	 * 独立房源入库
	 * */
	public function aloneHouse($result,$value){
	    Log::$format = '[ Y-m-d H:i:s ]';
	    Log::write("【未对比成功】【gov_id为：】".$value['id'],'INFO');
	    //权重 大于 0 但小于 35 把is_contrast = 5;
	    $data['is_contrast'] = 5;
	    //权重 等于 0 把is_contrast = 7;独立房源
	    if(empty($this->result)){$data['is_contrast'] = 7;}
	    $this->house_sell_gov->where('id='.$value['id'])->save($data);
	    //echo $this->house_sell_gov->getLastSql();
	    Log::write("【修改gov表状态】".$this->house_sell_gov->getLastSql(),'INFO');
	    //获取房源自增id
	    $new_num = $this->getAutoid();
	    $value['gov_id'] = $value['id'];
	    $value['id'] = $new_num;
	    $value['house_sell_id'] = $value['id'];
	    Log::write("【获取自增ID】".$new_num,'INFO');
	    Log::write("【获取gov_id】".$value['gov_id'],'INFO');
	    //做为一套新房源入库
	    //插入到house_sell表
	    $this->house_sell->data($value)->add();
	    //echo $this->house_sell->getLastSql();
	    Log::write("插入house_sell表【".$this->house_sell->getLastSql()."】",'INFO');
	    //插入到house_sell_info表
	    $this->house_sell_info->data($value)->add();
	    Log::write("插入house_sell_info表【".$this->house_sell_info->getLastSql()."】",'INFO');
	    //计算sell表中的cnt、min_price
	    $this->handleHouseCount($new_num);
	    Log::$format = '[ Y-m-d H:i:s ]';
	    Log::write("【计算house_sell表中的三个字段】【house_sell的id为：】".$new_num,'INFO');
        //插入到house_sell_pic表
        //室内图
//         $value['gov_id'] = $value['id'];
        $pic1 = array();
        $pic1 = explode('|',$value['house_pic_unit']);
        $value['house_pic_unit'] = $pic1;
        //户型图
        $pic2 = array();
        $pic2 = explode('|',$value['house_pic_layout']);
        $value['house_pic_layout'] = $pic2;
        //图片处理并入库
        $pic_start=microtime();
        Log::$format = '[ Y-m-d H:i:s ]';
        $this->handleHousePic($value);
        Log::$format = '[ Y-m-d H:i:s ]';
        $pic_end=microtime();
        Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
	}
	
	/*
	 * 有对比数据入库
	 * */
	public function moreHouse($arr_bath_insert,$value){
	    //模糊对比成功，将gov中的is_contrast字段改为3 
	    $data=array();
	    $data['is_contrast'] = 3;
	    $this->house_sell_gov->where('id='.$value['id'])->save($data);
	    //echo $this->house_sell_gov->getLastSql();
	    //echo "<br/>";
	    Log::$format = '[ Y-m-d H:i:s ]';
	    Log::write("【有对比数据】【gov_id为：】".$value['id'],'INFO');
	    Log::write("【将gov表中的is_contrast改为3】【gov_id为：】".$value['id'],'INFO');
	    //计算house_sell表中的新增字段
	    foreach($arr_bath_insert as $keys=>$str_insert){
	        $is_contrast=array();
	        $this->house_sell_info->data($str_insert)->add();
	        //更改gov表中的字段值is_contrast不等于1的设置为1
	        $where_contrast=array('id'=>$str_insert['gov_id']);
	        $is_contrast=$this->house_sell_gov->where($where_contrast)->field('is_contrast')->find();
	        if($is_contrast!=1){
	            Log::$format = '[ Y-m-d H:i:s ]';
	            Log::write("【对比成功的数据之前未对比成功，更改is_contrast为1】【gov_id为：】".$value['id'],'INFO');
	            $this->house_sell_gov->where($where_contrast)->save(array('is_contrast'=>1));
	        }
	        //计算sell表中的cnt、min_price
	        $this->handleHouseCount($str_insert['id']);
	        Log::$format = '[ Y-m-d H:i:s ]';
	        Log::write("【计算house_sell表中的三个字段】【house_sell的id为：】".$str_insert['id'],'INFO');
	    }
        $value['house_sell_id'] = $arr_bath_insert[0]['id'];
        //插入图片
        //插入到house_sell_pic表
        //室内图
        $value['gov_id'] = $value['id'];
        $pic1 = array();
        $pic1 = explode('|',$value['house_pic_unit']);
        $value['house_pic_unit'] = $pic1;
        //户型图
        $pic2 = array();
        $pic2 = explode('|',$value['house_pic_layout']);
        $value['house_pic_layout'] = $pic2;
        //图片处理并入库
        $pic_start=microtime();
        Log::$format = '[ Y-m-d H:i:s ]';
        $this->handleHousePic($value);
        Log::$format = '[ Y-m-d H:i:s ]';
        $pic_end=microtime();
        Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
	}
	
	/*
	 * 获取自增id
	 * */
	public function getAutoid() {
	    $autoid = new Model();
	    $ret = $autoid->execute('update auto_increment_id set auto_id = last_insert_id(auto_id+1) where id = 2');
	    if ($ret === false) {
	        return false;
	    } else {
	        return mysql_insert_id();
	    }
	}
	/*
	 * 处理楼层
	 * */
	public function handleFloor($value){
	    $is_num = is_numeric($value['house_floor']);
	    if($is_num){
	        $house_floor = $value['house_floor'];
	        if($value['house_floor']<($value['house_topfloor']/3)){
	            $value['house_floor'] = "低";
	        }elseif($value['house_floor']>=($value['house_topfloor']/3) && $value['house_floor']<=($value['house_topfloor']*(2/3))){
	            $value['house_floor'] = "中";
	        }else{
	            $value['house_floor'] = "高";
	        }
	        Log::write("【所在楼层是数字 已转换为 汉字】".$value['house_floor'],'INFO');
	        //用其他经纪公司的数据查 house_sell_info 表
	        //	                $result = $model->table(array('house_sell_copy'=>'a','house_sell_info_copy'=>'b'))
	        $this->result = $this->house_sell->table(array('house_sell'=>'a','house_sell_info'=>'b'))
	        ->field('a.*,b.*')
	        ->where('a.id=b.id and b.house_topfloor='.$value['house_topfloor'].
	            ' and( b.house_floor = '.$house_floor.
	            ' or b.house_floor like "%'.$value['house_floor'].'%") and a.borough_id = '.$value['borough_id'].
	            ' and a.house_room = '.$value['house_room'].
	            ' and (
                                                        (a.house_totalarea >= '.$value['house_totalarea'].'-1'.
	            ' and a.house_totalarea <='.$value['house_totalarea'].'+1
                                                        )
                                                        or a.house_totalarea ='.$value['house_totalarea'].'
                                                ) and b.source != '.$value['source']
	        )
	        ->group('b.id')
	        ->select();
	        Log::write("【用gov表数据做条件查 house_sell_info 表】".$this->house_sell->getLastSql(),'INFO');
	    }else{
	        preg_match("/(中|高|低).*/",$value['house_floor'], $floor);
	        $value['house_floor'] = $floor[1];
	        //如果是汉字换算成数字
	        if ($value['house_floor'] == '低'){
	            $min = -1;
	            $max = ($value['house_topfloor']/3);
	        }elseif ($value['house_floor'] == '中'){
	            $min = ($value['house_topfloor']/3);
	            $max = ($value['house_topfloor']*(2/3));
	        }elseif($value['house_floor'] == '高'){
	            $min = ($value['house_topfloor']*(2/3));
	            $max = $value['house_topfloor'];
	        }else{
	            $min = -999;
	            $max = -99;
	        }
	        Log::write("【所在楼层是汉字 已转换为 数字区间】".$min."至".$max,'INFO');
	        //用其他经纪公司的数据查 house_sell_info 表
	        $this->result = $this->house_sell->table(array('house_sell'=>'a','house_sell_info'=>'b'))
	        ->field('a.*,b.*')
	        ->where('a.id=b.id and b.house_topfloor='.$value['house_topfloor'].
	            ' and( b.house_floor like "%'.$value['house_floor'].
	            '%" or (b.house_floor>='.$min.' and b.house_floor<='.$max.')) and a.borough_id = '.$value['borough_id'].
	            ' and a.house_room = '.$value['house_room'].
	            ' and (
                                                        (a.house_totalarea >= '.$value['house_totalarea'].'-1'.
	            ' and a.house_totalarea <='.$value['house_totalarea'].'+1
                                                        )
                                                        or a.house_totalarea ='.$value['house_totalarea'].'
                                                ) and b.source != '.$value['source']
	        )
	        ->group('b.id')
	        ->select();
// 	        log::write("【用gov表数据做条件查 house_sell_info 表】".$this->house_sell->getLastSql(),'INFO');
	    }
	}

    protected function connect_redis(){
        /*******连接redis******/
        $this->redis = new Redis();
        $this->redis->connect(C('redis_server'), C('redis_port'));
        $this->redis->auth(C('redis_auth'));
        $this->redis->select(C('redis_db'));
        /*******连接redis******/
    }

	/*
	 *计算 权重
	 * */
	public function countWeight ($result,$value){
	    foreach($result as $k=>$v){
	        $weight=0;
	        //最大权重的房源
	        $value_temp=array();
	        $v_temp=array();
	        Log::write("house_sell_info房源ID【".$v['id']."】与 其他经纪公司房源ID【".$value['id']."】对比开始",'INFO');
	        //对比厅
	        if($v['house_hall'] == $value['house_hall']){
	            $weight +='31.00';
	            Log::write("对比厅后，权重：【".$weight."】",'INFO');
	        }
	        //对比房屋价格 上下浮动5%
	        if($value['house_price']*0.95<=$v['house_price'] &&  $v['house_price']<= $value['house_price']*1.05){
	            $weight_price=(1-abs($value['house_price']-$v['house_price'])/$value['house_price']/0.05)*25.00;
	            $weight_price=$weight_price==0?1:$weight_price;
	            //容错
	            $weight_price=$weight_price>25?25:$weight_price;
	            $weight +=$weight_price;
	            Log::write("对比价格后，权重：【".$weight."】",'INFO');
	        }else{
	            $weight_price=(1-abs($value['house_price']-$v['house_price'])/$value['house_price']/0.05)*60.00;
	            $weight_price=$weight_price==0?1:$weight_price;
	            $weight_price=$weight_price<-60?-60:$weight_price;
	            $weight +=$weight_price;
	            Log::write("对比价格后，权重：【".$weight."】",'INFO');
	        }
	        //对比装修情况
	        if($v['house_fitment'] == $value['house_fitment']){
	            $weight +='25.00';
	            Log::write("对比装修后，权重：【".$weight."】",'INFO');
	        }
	        //对比朝向
	        if(self::tVt($v['house_toward'],$value['house_toward'])){
	            $weight +='12.00';
	            Log::write("对比朝向后，权重：【".$weight."】",'INFO');
	        }
	        //对比卫生间
	        if($v['house_toilet'] == $value['house_toilet']){
	            $weight +='7.00';
	            Log::write("对比卫生间后，权重：【".$weight."】",'INFO');
	        }
	        if($weight >='35.00'){
	            /**
	             * $value_temp 是 info 表中的数据
	             * $v_temp 是 gov表中的数据
	             */
	            $value_temp = $value;
	            $v_temp = $v;
	           
	            Log::write("总权重值:【".$weight."】",'INFO');
	    
	            $value_temp['gov_id'] = $value_temp['id'];
	            $value_temp['id'] = $v_temp['id'];
	            $value_temp['weight'] = $weight;
	            $this->tharr_bath_insert[]=$value_temp;
	        }
	    }
	    return  $this->tharr_bath_insert;
	}
	
	/**
	 * 房源图片保存(house_sell_info表使用)
	 */
	function handleHousePic($info){
	    //房源图片表
	    $house_sell_pic = M('house_sell_pic');
	    $pic_info = array();
	    if($info){
	        foreach($info as $k=>$v){
	            //房间图
	            $pic_info = array();
	            $tmpPicSelect=array();
	            if($k == 'house_pic_unit' && $v[0]!=''){
	                foreach($v as $value){
	                    //数据库中是否存在，如果存在获取id，不存在插入图片地址表中；
	                    $tmpPicSelect=array(
	                        'pic_source_url'=>$value,
	                    );
	                    $picId = $house_sell_pic->where($tmpPicSelect)->find();
	                    if(!$picId){
	                        $fhunit = file_get_contents($value);
	                        $time = microtime(true)*10000;
	                        $ypy_path = '/sell/unit/'.$time.'.jpg';
	                        $rsp1 = $this->upyun->writeFile($ypy_path, $fhunit, true);   // 上传图片，自动创建目录
	                        $endTime = microtime(true);
	                        $pic_path = 'http://zhaijiso-pic.b0.upaiyun.com'.$ypy_path;
	
	                        $pic_info[]=array(
	                            'pic_source_url'=>$value,
	                            'pic_url'=>$pic_path,
	                            'house_sell_id'=>$info['house_sell_id'],
	                            'gov_id'=>$info['gov_id'],
	                            'pic_type'=>1,
	                            'time'=>date('Y-m-d h:i:s',time())
	                        );
	                    }
	                    //						$info[$i]['house_pic_layout'] = empty($info[$i]['house_pic_layout'])?$pic_unit_id:$info[$i]['house_pic_layout'].",".$pic_unit_id;
	                }
	                $house_sell_pic->addAll($pic_info);
	            }
	            //户型图
	            $pic_info = array();
	            $tmpPicSelect=array();
	            if($k == "house_pic_layout"  && $v[0]!=''){
	                foreach($v as $value){
	                    //数据库中是否存在，如果存在获取id，不存在插入图片地址表中；
	                    $tmpPicSelect=array(
	                        'pic_source_url'=>$value,
	                    );
	
	                    $picId = $house_sell_pic->where($tmpPicSelect)->find();
	                    if(!$picId){
	
	                        $fhunit = file_get_contents($value);
	                        $time = microtime(true)*10000;
	                        $ypy_path = '/sell/layout/'.$time.'.jpg';
	                        $rsp1 = $this->upyun->writeFile($ypy_path, $fhunit, true);   // 上传图片，自动创建目录
	                        $endTime = microtime(true);
	                        $pic_path = 'http://zhaijiso-pic.b0.upaiyun.com'.$ypy_path;
	                        $pic_info[]=array(
	                            'pic_source_url'=>$value,
	                            'pic_url'=>$pic_path,
	                            'house_sell_id'=>$info['house_sell_id'],
	                            'gov_id'=>$info['gov_id'],
	                            'pic_type'=>2,
	                            'time'=>date('Y-m-d h:i:s',time())
	                        );
	                    }
	                    //						$info[$i]['house_pic_layout'] = empty($info[$i]['house_pic_layout'])?$pic_unit_id:$info[$i]['house_pic_layout'].",".$pic_unit_id;
	                }
	                $house_sell_pic->addAll($pic_info);
	            }
	
	        }
	    }
	}
    /*
     * 计算cnt,minprice,is_fangzhu
     * */
    public function handleHouseCount($house_id){
    
        if(empty($house_id)){
            $house_id = $_GET['house_id'];
        }
        if(empty($house_id)){
            exit;
        }
        
	    // 出售房源主表
	    $model_sell = M ( 'house_sell' );
	    // 出售房源info表
	    $model_info = M ( 'house_sell_info' );
	    $model_info_delete = M ( 'house_sell_info_delete' );
	    $house_id=trim($house_id);
	    $where=array('id'=>$house_id);
	    $house_list = $model_sell->where($where)->select();
	    
	    $count = 0;
		if(!empty($house_list[0])){
	        foreach ( $house_list as $key => $value ) {
	            // 最近时间
	            $last_time = 0;
	            // 最低价格
	            $min_price = 100000000000;
	            $house_info_result = array ();
	            $arr_del_id_gov=array();
	            // 查询house_sell_info表
	            $where = array ();
	            $where = array (
	                'id' => $value ['id']
	            );
	            $house_info_list = $model_info->where ( $where )->order ( 'source' )->field ( 'id,source,updated,house_price,gov_id' )->select ();
	            
	            
	            // 查询sell_info表中的数据,查找出每个中介公司中最新的房源
	            foreach ( $house_info_list as $k => $v ) {
	                if ($house_info_result [$v ['source']] ['updated'] < $v ['updated']) {
	                    $house_info_result [$v ['source']] = $v;
	                }else{
	                    $arr_del_id_gov[]=$v;
	                }
	            }
	            // 从筛选出来的每个中介公司最新的一条房源中找到整个房源的最低价格和最近更新时间
	            foreach ( $house_info_result as $k1 => $v1 ) {
	                if($last_time<$v1['updated']){
	                    $last_time = $v1 ['updated'];
	                }
	                if ($min_price > $v1 ['house_price']) {
	                    $min_price = $v1 ['house_price'];
	                }
	            }
	            // 删除house_sell_info中数据(id= 并且 sourcre= 并且 updated！= )
	            $num_fangzhu=0;
	            $is_fangzhu = 0;
	            foreach ( $house_info_result as $k2 => $v2 ) {
	                if($v2['source'] == 10){
	                    $num_fangzhu=1;
						$is_fangzhu = 1;
					}
					/*
	                $where = array ();
	                $insert_delete_result = array ();
	                $where ['id'] = $v2 ['id'];
	                $where ['source'] = $v2 ['source'];
	                $where ['updated'] = array (
	                    'neq',
	                    $v2 ['updated']
	                );
	                $insert_delete_result = $model_info->where ( $where )->select();
	                */
					/** 同一套房源同一个经济公司公司有多套房源不删除
	                if (! empty ( $arr_del_id_gov )) {
	                    $model_info_delete->addAll ( $arr_del_id_gov );
	                    $model_info->where ( $arr_del_id_gov )->delete ();
	                }
	                **/
	            }
                    // 如果没有变化，则sell表不做更改
                if (($value['min_price'] == $min_price) && ($value['updated'] == $last_time) && ($value['is_fangzhu'] == $is_fangzhu) && ($value['cnt'] == count($house_info_result) + $num_fangzhu)) {
                    continue;
                } 
                elseif ($min_price != 100000000000 && $last_time != 0) { // 增加house_sell 2字段值
                        $value['min_price'] = $min_price;
                        $value['updated'] = $last_time;
                        $value['is_fangzhu'] = $is_fangzhu;
                        $value['cnt'] = count($house_info_result) + $num_fangzhu;
                        $model_sell->save($value);
                        Log::write("【更新sell中cnt,minprice,updated】" . $model_sell->getLastSql(), 'INFO');
                    }
            }
        }
	}
	
	/**
	 * 计算sell表中 cnt为0的数据
	 */
	
	public function handleCntHouse(){

	    // 出售房源主表
	    $model_sell = M ( 'house_sell' );
	    // gov数据
	    $count_num = $model_sell->where(array('cnt'=>0))->count ();
	    $listCount = ($count_num/100) == 0 ? ($count_num/100) : ($count_num/100 + 1);
	     
	    for($intNum = 0; $intNum <= $listCount; $intNum ++) {
	        $left = $intNum * 1000;
	        // 			$house_list = $model_sell->where(array('id'=>58948))->limit ( "$left,100" )->select ();
	        $house_list = $model_sell->limit ( "$left,1000" )->where(array('cnt'=>0))->field('id')->select ();
	        $count = 0;
	        if(!empty($house_list)){
	            foreach ( $house_list as $key => $value ) {
	                $this->handleHouseCount($value['id']);
	                sleep(2);
	            }
	        }
	    }
	    
	}
	/*
	 * 计算sell表中的  updated,minprice,cnt,is_fangzhu
	 */
	public function handleHouse() {
	    // 出售房源主表
	    $model_sell = M ( 'house_sell' );
	    // gov数据
	    $count_num = $model_sell->count ();
	    
	    $listCount = ($count_num/1000) == 0 ? ($count_num/1000) : ($count_num/1000 + 1);
	    
	    for($intNum = 0; $intNum <= $listCount; $intNum ++) {
	        $left = $intNum * 1000;
	        // 			$house_list = $model_sell->where(array('id'=>58948))->limit ( "$left,100" )->select ();
// 	        $house_list = $model_sell->where("min_price is null or cnt = 0 or updated is null")->order("created")->limit ( "$left,1000" )->field('id')->select ();
	        $house_list = $model_sell->order("id")->limit ( "$left,1000" )->field('id')->select ();
	        $count = 0;
	        if(!empty($house_list)){
	            foreach ( $house_list as $key => $value ) {
	                
	                $this->handleHouseCount($value['id']);
	            }
	        }
	    }
	}
	
	public function handleSellExHouse() {
	    // 出售房源主表
	    $model_sell = M ( 'house_sell' );
	    // gov数据
	    $count_num = $model_sell->count ();
	     
	    $listCount = ($count_num/1000) == 0 ? ($count_num/1000) : ($count_num/1000 + 1);
	     
	    for($intNum = 0; $intNum <= $listCount; $intNum ++) {
	        $left = $intNum * 1000;
	        // 			$house_list = $model_sell->where(array('id'=>58948))->limit ( "$left,100" )->select ();
	        $house_list = $model_sell->limit ( "$left,1000" )->where("min_price is null")->field('id')->select ();
	        $count = 0;
	        if(!empty($house_list)){
	            foreach ( $house_list as $key => $value ) {
	                $this->handleHouseCount($value['id']);
	            }
	        }
	    }
	}

	
    
	    /*
	     * 朝向对比时只要有一个方向相同就认为朝向相同
	     * */
	    public function tVt($t1,$t2){
	        if($t1=='1'){
	            if($t2 == '1' || $t2 == '5' || $t2 == '7' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '14' || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='2'){
	            if($t2 == '2' || $t2 == '6' || $t2 == '8' || $t2 == '10' || $t2 == '11' || $t2 == '13' || $t2 == '14' || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='3'){
	            if($t2 == '3' || $t2 == '5' || $t2 == '6' || $t2 == '9' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='4'){
	            if($t2 == '4' || $t2 == '7' || $t2 == '8' || $t2 == '9' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='5'){
	            if($t2 == '5' || $t2 == '1' || $t2 == '3' || $t2 == '6' || $t2 == '7' || $t2 == '9' || $t2 == '10'  || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='6'){
	            if($t2 == '6' || $t2 == '2' || $t2 == '3' || $t2 == '5' || $t2 == '8' || $t2 == '9' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='7'){
	            if($t2 == '7' || $t2 == '1' || $t2 == '4' || $t2 == '5' || $t2 == '8' || $t2 == '9' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='8'){
	            if($t2 == '8' || $t2 == '2' || $t2 == '4' || $t2 == '6' || $t2 == '7' || $t2 == '9' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='9'){
	            if($t2 == '9' || $t2 == '3' || $t2 == '4' || $t2 == '5' || $t2 == '6' || $t2 == '7' || $t2 == '8' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='10'){
	            if($t2 == '10' || $t2 == '1' || $t2 == '2' || $t2 == '5' || $t2 == '6' || $t2 == '7' || $t2 == '8' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='11'){
	            if($t2 !== '4'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='12'){
	            if($t2 !== '2'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='13'){
	            if($t2 !== '1'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='14'){
	            if($t2 !== '3'){
	                return true;
	            }else{
	                return false;
	            }
	        }
	        if($t1=='15'){
	            return true;
	        }
	    }
	    
/*
 *	用其他经纪公司数据   精确所在楼层    对比 house_sell 表中的数据
 *	大于35%，则更新house_sell_info表中的数据
 *	否则，作为一个单独的房源入库
 * */
	    public function fangzhuVsother(){
	        $this->upyun = new UpYun('zhaijiso-pic', 'kys', 'zjsZJS123');
	        //提供房源自增id表
	        $auto = M('auto_increment_id');
	        //出售房源主表
	        //	    $model=M('house_sell_copy');
	        $model=M('house_sell');
	        //出售房源信息表
	        //	    $model_info = M('house_sell_info_copy');
	        $model_info = M('house_sell_info');
	        //房源图片表
	        //	    $house_sell_pic = M('house_sell_pic_copy');
	        $house_sell_pic = M('house_sell_pic');
	        //经纪公司官网表
	        //	    $house_sell_gov = M('house_sell_gov_copy');
	        $house_sell_gov = M('house_sell_gov');
	         
	        // 	    $listCount=$house_sell_gov->where('is_contrast = 2 and source = 10')->count();
	        //需要处理次数
	        // 	    $listCount = $listCount/100;
	        // 	    for($intNum=0;$intNum<$listCount;$intNum++){
	        // 	        $left=$intNum*100;
	        //查出gov表中 fangzhu网未对比的数据
	        $list = array();
	        $list=$house_sell_gov->where('is_contrast = 2 and source = 10')->limit("0,100")->select();
	        // 	        dump($list);
	        while(!empty($list)){
	            foreach($list as $key=>$value){
	                $value['created'] = microtime(true);
	                $value['updated'] = microtime(true);
	                $weight_tmp = 0;
	                $candidate=array();
	                $result=array();
	                //最低价格
	                // 	                $min_price = 100000000000;
	                // 最近时间
	                // 	                $last_time = 0;
	                //不同中介公司的房源数目
	                // 	                $cnt = 0;
	                $is_num = is_numeric($value['house_floor']);
	                if($is_num){
	                    $house_floor = $value['house_floor'];
	                    if($value['house_floor']<($value['house_topfloor']/3)){
	                        $value['house_floor'] = "低";
	                    }elseif($value['house_floor']>=($value['house_topfloor']/3) && $value['house_floor']<=($value['house_topfloor']*(2/3))){
	                        $value['house_floor'] = "中";
	                    }else{
	                        $value['house_floor'] = "高";
	                    }
	                    Log::write("【所在楼层是数字 已转换为 汉字】".$value['house_floor'],'INFO');
	                    //用其他经纪公司的数据查 house_sell_info 表
	                    //	                    $result = $model->table(array('house_sell_copy'=>'a','house_sell_info_copy'=>'b'))
	                    $result = $model->table(array('house_sell'=>'a','house_sell_info'=>'b'))
	                    ->field('a.*,b.*')
	                    ->where('a.id=b.id and b.house_topfloor='.$value['house_topfloor'].
	                        ' and( b.house_floor = '.$house_floor.
	                        ' or b.house_floor like "%'.$value['house_floor'].'%") and a.borough_id = '.$value['borough_id'].
	                        ' and a.house_room = '.$value['house_room'].
	                        ' and (
                                                        (a.house_totalarea >= '.$value['house_totalarea'].'-1'.
	                        ' and a.house_totalarea <='.$value['house_totalarea'].'+1
                                                        )
                                                        or a.house_totalarea ='.$value['house_totalarea'].'
                                                ) and b.source != '.$value['source']
	                    )
	                    ->group('b.id')
	                    ->select();
	                    Log::write("【用gov表数据做条件查 house_sell_info 表】".$model->getLastSql(),'INFO');
	                }else{
	                    preg_match("/(中|高|低).*/",$value['house_floor'], $floor);
	                    $value['house_floor'] = $floor[1];
	                    //如果是汉字换算成数字
	                    if ($value['house_floor'] == '低'){
	                        $min = -1;
	                        $max = ($value['house_topfloor']/3);
	                    }elseif ($value['house_floor'] == '中'){
	                        $min = ($value['house_topfloor']/3);
	                        $max = ($value['house_topfloor']*(2/3));
	                    }elseif($value['house_floor'] == '高'){
	                        $min = ($value['house_topfloor']*(2/3));
	                        $max = $value['house_topfloor'];
	                    }else{
	                        $min = -999;
	                        $max = -99;
	                    }
	                     
	                    Log::write("【所在楼层是汉字 已转换为 数字区间】".$min."至".$max,'INFO');
	                    //用其他经纪公司的数据查 house_sell_info 表
	                    //    	                $result = $model->table(array('house_sell_copy'=>'a','house_sell_info_copy'=>'b'))
	                    $result = $model->table(array('house_sell'=>'a','house_sell_info'=>'b'))
	                    ->field('a.*,b.*')
	                    ->where('a.id=b.id and b.house_topfloor='.$value['house_topfloor'].
	                        ' and( b.house_floor like "%'.$value['house_floor'].
	                        '%" or (b.house_floor>='.$min.' and b.house_floor<='.$max.')) and a.borough_id = '.$value['borough_id'].
	                        ' and a.house_room = '.$value['house_room'].
	                        ' and (
                                                            (a.house_totalarea >= '.$value['house_totalarea'].'-1'.
	                        ' and a.house_totalarea <='.$value['house_totalarea'].'+1
                                                            )
                                                            or a.house_totalarea ='.$value['house_totalarea'].'
                                                    ) and b.source != '.$value['source']
	                    )
	                    ->group('b.id')
	                    ->select();
	                    log::write("【用gov表数据做条件查 house_sell_info 表】".$model->getLastSql(),'INFO');
	                }
	                //$v    info 表中的数据
	                //$value  gov 表中  source = 10  fangzhur网的数据
	    
	                if($result){
	                    //放置最大的中介公司的该套房源信息
	                    $candidate = $result[0];
	                    //寻找全局最大匹配度
	                    foreach($result as $k=>$v){
	                        if($v['cityarea_id'] == null){
	                            //房主网抓到的数据很多数据没有  cityarea_id 这样的数据暂时不处理，把gov表中状态改为 8  是无法对比数据
	                            $data['is_contrast'] = 8;
	                            $house_sell_gov->where('id='.$value['id'])->save($data);
	                        }else{
	                            $weight=0;
	                            //	                            $value_temp=array();
	                            $v_temp=array();
	                            Log::write("house_sell_info房源ID【".$v['id']."】与 其他经纪公司房源ID【".$value['id']."】对比开始",'INFO');
	                            //对比厅
	                            if($v['house_hall'] == $value['house_hall']){
	                                $weight +='31.00';
	                                Log::write("对比厅后，权重：【".$weight."】",'INFO');
	                            }
	                           //对比房屋价格 上下浮动5%
	                           if($value['house_price']*0.95<=$v['house_price'] &&  $v['house_price']<= $value['house_price']*1.05){
                    	            $weight_price=(1-abs($value['house_price']-$v['house_price'])/$value['house_price']/0.05)*25.00;
                    	            $weight_price=$weight_price==0?1:$weight_price;
                    	            //容错
                    	            $weight_price=$weight_price>25?25:$weight_price;
                    	            $weight +=$weight_price;
                    	            Log::write("对比价格后，权重：【".$weight."】",'INFO');
	                           }else{
                    	            $weight_price=(1-abs($value['house_price']-$v['house_price'])/$value['house_price']/0.05)*60.00;
                    	            $weight_price=$weight_price==0?1:$weight_price;
                    	            $weight_price=$weight_price<-60?-60:$weight_price;
                    	            $weight +=$weight_price;
                    	            Log::write("对比价格后，权重：【".$weight."】",'INFO');
                    	        }
	                            //对比装修情况
	                            if($v['house_fitment'] == $value['house_fitment']){
	                                $weight +='25.00';
	                                Log::write("对比装修后，权重：【".$weight."】",'INFO');
	                            }
	                            //对比朝向
	                            if(self::tVt($v['house_toward'],$value['house_toward'])){
	                                $weight +='12.00';
	                                Log::write("对比朝向后，权重：【".$weight."】",'INFO');
	                            }
	                            //对比卫生间
	                            if($v['house_toilet'] == $value['house_toilet']){
	                                $weight +='7.00';
	                                Log::write("对比卫生间后，权重：【".$weight."】",'INFO');
	                            }
	                            Log::write("第".$k."套房源, 权重：".$weight, "INFO");
	                            //筛选最大的房源
	                            if($weight_tmp < $weight){
	                                $candidate = $v;
	                                $weight_tmp = $weight;
	                            }
	                            // 	                            if($weight>=35){
	                            // 	                                $cnt++;
	                            // 	                            }
	                        }
	                    }
	                    //如果全局最大匹配度大于35则对比匹配成功，入库
	                    if($weight_tmp >= 35){
	                        Log::write("房源最大的权重：".$weight_tmp, "INFO");
	                        Log::write("总权重值:【".$weight_tmp."】",'INFO');
	                        //把对比成功的fangzhur的数据 is_contrast 改为3对比成功
	                        $data['is_contrast'] = 3;
	                        $house_sell_gov->where('id='.$value['id'])->save($data);
	                        Log::write(":修改已对比过的数据状态【".M()->getLastSql()."】",'INFO');
	                        //把对比成功的fangzhur网的数据 插入到  house_sell_info
	                        $value['gov_id'] = $value['id'];
	                        $value['id'] = $candidate['id'];
	                        $value['weight'] = $weight_tmp;
	                        $value['created'] = microtime(true);
	                        $model_info->data($value)->add();
	                        $this->handleHouseCount($candidate['id']);
// 	                        R('houseHandle/handleHouseCount',array('id'=>$candidate['id']));
	                        Log::write("插入house_sell_info表【".$model_info->getLastSql()."】",'INFO');
	    
	                        //判断房主网图片是否有效
	                        //	                        if($value['house_thumb'] != 'http://www.fangzhur.com/upfile/' || $value['house_thumb'] != 'http://123.57.61.107/manager/Public/images/default.png' ){
	                        //有效则处理并入库
	                        $pic = explode('|',$value['house_pic_unit']);
	                        $pic2 = explode('|',$value['house_pic_layout']);
	                        $value_temp['house_pic_unit'] = $pic;
	                        $value_temp['house_pic_layout'] = $pic2;
	                        $pic_start=microtime();
	                        Log::$format = '[ Y-m-d H:i:s ]';
	                        $this->handleHousePic($value_temp);
	                        Log::$format = '[ Y-m-d H:i:s ]';
	                        $pic_end=microtime();
	                        Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
	                        //	                        }
	                        $end=microtime();
	                        Log::$format = '[ Y-m-d H:i:s ]';
	                        Log::write("【  查找，对比，修改，插入 总耗时】".($end-$start),'INFO');
	                    }
	                    else{
	                        //全局最大匹配度小于35，则作为一套独立房源入库
	                        //把未对比成功的其他经纪公司的数据 is_contrast 改为 5 模糊对比是无可对比房源
	                        $arr_house=$value;
	                        $data['is_contrast'] = 5;
	                        $house_sell_gov->where('id='.$value['id'])->save($data);
	                        Log::write("【修改gov表状态】".M()->getLastSql(),'INFO');
	                        //获取房源自增id
	                        // 	                        $auto->where("name = 'house_sell'")->setInc('auto_id','1');
	                        // 	                        $new_num = $auto->where("name = 'house_sell'")->getField('auto_id');
	                        $new_num = $this->getAutoid();
	                        $arr_house['id']=$new_num;
	                        $arr_house['gov_id']=$value['id'];
	                        $arr_house['cnt']=1;
	                        $arr_house['created'] = microtime(true);;
	                        $arr_house['updated'] = microtime(true);;
	                        $arr_house['house_desc'] = trimall($arr_house['house_desc']);
	                        $arr_house['weight'] = $weight_tmp;
	                        Log::write("【获取自增ID】".$new_num,'INFO');
	                        Log::write("【获取gov_id】".$value['gov_id'],'INFO');
	                        //做为一套新房源入库
	                        //插入到house_sell表
	                        $model->data($arr_house)->add();
	                        Log::write("插入house_sell表【".$model->getLastSql()."】",'INFO');
	                        //插入到house_sell_info表
	                        $model_info->data($arr_house)->add();
	                        Log::write("插入house_sell_info表【".$model_info->getLastSql()."】",'INFO');
	    
// 	                        R('houseHandle/handleHouseCount',array('id'=>$new_num));
	                        $this->handleHouseCount($new_num);
	                        //判断房主网图片是否有效
	                        //	                        if($value['house_thumb'] != 'http://www.fangzhur.com/upfile/' || $value['house_thumb'] != 'http://123.57.61.107/manager/Public/images/default.png' ){
	                        //有效则处理并入库
	                        $pic = explode('|',$value['house_pic_unit']);
	                        $pic2 = explode('|',$value['house_pic_layout']);
	                        $arr_house['house_pic_unit'] = $pic;
	                        $arr_house['house_pic_layout'] = $pic2;
	                        $pic_start=microtime();
	                        Log::$format = '[ Y-m-d H:i:s ]';
	                        $this->handleHousePic($arr_house);
	                        Log::$format = '[ Y-m-d H:i:s ]';
	                        $pic_end=microtime();
	                        Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
	                        //	                        }
	                    }
	                }else{
	                    //无可对比房源，则作为一套独立房源入库
	                    //把未对比成功的其他经纪公司的数据 is_contrast 改为 7 模糊对比是无可对比房源
	                    $arr_house=$value;
	                    if($arr_house['cityarea_id'] == null){
	                        //房主网抓到的数据很多数据没有  cityarea_id 这样的数据暂时不处理，把gov表中状态改为 8  是无法对比数据
	                        $data['is_contrast'] = 8;
	                        $house_sell_gov->where('id='.$value['id'])->save($data);
	                    }else{
	                        $data['is_contrast'] = 7;
	                        $house_sell_gov->where('id='.$value['id'])->save($data);
	                        Log::write("【修改gov表状态】".M()->getLastSql(),'INFO');
	                        //获取房源自增id
	                        // 	                        $auto->where("name = 'house_sell'")->setInc('auto_id','1');
	                        // 	                        $new_num = $auto->where("name = 'house_sell'")->getField('auto_id');
	                        $new_num = $this->getAutoid();
	                        $arr_house['id']=$new_num;
	                        $arr_house['gov_id']=$value['id'];
	                        $arr_house['cnt']=1;
	                        $arr_house['created'] = microtime(true);
	                        $arr_house['updated'] = microtime(true);
	                        $arr_house['house_desc'] = trimall($arr_house['house_desc']);
	                        Log::write("【获取自增ID】".$new_num,'INFO');
	                        Log::write("【获取gov_id】".$value['gov_id'],'INFO');
	                        //做为一套新房源入库
	                        //插入到house_sell表
	                        $model->data($arr_house)->add();
	                        Log::write("插入house_sell表【".$model->getLastSql()."】",'INFO');
	                        //插入到house_sell_info表
	                        $model_info->data($arr_house)->add();
	                        Log::write("插入house_sell_info表【".$model_info->getLastSql()."】",'INFO');
	                         
// 	                        R('houseHandle/handleHouseCount',array('id'=>$new_num));
	                        $this->handleHouseCount($new_num);
	                        //判断房主网图片是否有效
	                        //	                        if($value['house_thumb'] != 'http://www.fangzhur.com/upfile/' || $value['house_thumb'] != 'http://123.57.61.107/manager/Public/images/default.png' ){
	                        //有效则处理并入库
	                        $pic = explode('|',$value['house_pic_unit']);
	                        $pic2 = explode('|',$value['house_pic_layout']);
	                        $arr_house['house_pic_unit'] = $pic;
	                        $arr_house['house_pic_layout'] = $pic2;
	                        $pic_start=microtime();
	                        Log::$format = '[ Y-m-d H:i:s ]';
	                        $this->handleHousePic($arr_house);
	                        Log::$format = '[ Y-m-d H:i:s ]';
	                        $pic_end=microtime();
	                        Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
	                        //	                        }
	                    }
	                }
	            }
	            usleep(5000);
	            $list=$house_sell_gov->where('is_contrast = 2 and is_fill=2 and source = 10')->limit("0,100")->select();
	        }
	    }	    
	    
	    
	    
	    
	    
	    
	    
	    
	    
}
